﻿using System;
using System.Xml;
using System.Collections;
using RAP.Framework.Libary.Utils.Collections.Dictionary;

namespace RAP.Framework.Libary.Utils
{
	/// <summary>
	/// XmlAttributeCollection扩展
	/// </summary>
	public static partial class Extensions
	{
		#region XmlAttributeCollection扩展

		/// <summary>
		/// 将指定名称的XML属性的值转换成字符串类型
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static string ToString ( this XmlAttributeCollection xmlAttrs, string key, string defaultValue = "" )
		{
			if ( xmlAttrs[ key ] == null )
			{
				return defaultValue;
			}
			return xmlAttrs[ key ].Value;
		}

		/// <summary>
		/// 将指定名称的XML属性的值转换成整数类型
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Int32 ToInt32 ( this XmlAttributeCollection xmlAttrs, string key, int defaultValue = 0 )
		{
			if ( xmlAttrs[ key ] == null )
			{
				return defaultValue;
			}
			return xmlAttrs[ key ].Value.ToInt32Value ( );
		}

		/// <summary>
		/// 将指定名称的XML属性的值转换成Boolean类型
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static bool ToBoolean ( this XmlAttributeCollection xmlAttrs, string key, bool defaultValue = false )
		{
			if ( xmlAttrs[ key ] == null )
			{
				return defaultValue;
			}
			return xmlAttrs[ key ].Value.ToBooleanValue ( );
		}

		/// <summary>
		/// 将指定名称的XML属性的值转换成双精度类型
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Double ToDouble ( this XmlAttributeCollection xmlAttrs, string key, double defaultValue = 0.0d )
		{
			if ( xmlAttrs[ key ] == null )
			{
				return defaultValue;
			}
			return xmlAttrs[ key ].Value.ToDoubleValue ( );
		}

		/// <summary>
		/// 将指定名称的XML属性的值转换成浮点类型
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static float ToFloat ( this XmlAttributeCollection xmlAttrs, string key, float defaultValue = 0.0f )
		{
			if ( xmlAttrs[ key ] == null )
			{
				return defaultValue;
			}
			return xmlAttrs[ key ].Value.ToFloatValue ( );
		}

		/// <summary>
		/// 将指定名称的XML属性的值转换成日期类型
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static DateTime? ToDateTime ( this XmlAttributeCollection xmlAttrs, string key, DateTime? defaultValue = null )
		{
			if ( xmlAttrs[ key ] == null )
			{
				return defaultValue;
			}
			return xmlAttrs[ key ].Value.ToDateTimeValue ( );
		}

		/// <summary>
		/// 将XmlAttributes集合中的属性转换成键值集合
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <returns></returns>
		public static IDictionary ToDictionary ( this XmlAttributeCollection xmlAttrs )
		{
			IDictionary attrs = new ListDictionary ( );
			xmlAttrs.ToDictionary ( attrs );
			return attrs;
		}

		/// <summary>
		/// 将指定的Xml节点对象的属性转换到指定的IDictionary对象中
		/// </summary>
		/// <param name="xmlAttrs"></param>
		/// <param name="target"></param>
		public static void ToDictionary ( this XmlAttributeCollection xmlAttrs, IDictionary target )
		{
			if ( xmlAttrs == null )
			{
				return;
			}
			foreach ( XmlAttribute attr in xmlAttrs )
			{
				target[ attr.Name ] = attr.Value;
			}
		}

		/// <summary>
		/// 扩展AttributeCollection的ToEnumValue方法，将指定属性转换成指定枚举值
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="atts"></param>
		/// <param name="name"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static T ToEnumValue<T>( this XmlAttributeCollection atts, string name, T defaultValue = default ( T ) )
		{
			if ( atts[ name ] == null )
			{
				return defaultValue;
			}
			string value = atts[ name ].Value;
			if ( string.IsNullOrEmpty ( value ) )
			{
				return defaultValue;
			}
			return ( T )System.Enum.Parse ( typeof ( T ), value );
		}

		#endregion XmlAttributeCollection扩展
	}
}
